package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LuckyHistoryBo;
import com.yiqi.lottery.feature.football.adapter.FootballMatchSelectedAdapter;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.login.view.LoginActivity;
import com.yiqi.lottery.feature.lottery.contract.LottoContract;
import com.yiqi.lottery.feature.lottery.data.LotteryData;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.DaggerLottoComponent;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.LottoPresenterModule;
import com.yiqi.lottery.feature.mine.view.BillingActivity;
import com.yiqi.lottery.framework.DataServer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class FootballMatchCalculateMoneyActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, LottoContract.View {

    @Inject
    LottoContract.Presenter mPresenter;
    @BindView(R.id.tv_back)
    TextView tv_back;
    @BindView(R.id.tv_award_youhua)
    TextView tv_award_youhua;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_hemai)
    TextView tv_hemai;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_award)
    TextView tv_award;
    @BindView(R.id.tv_buy)
    TextView tv_buy;
    @BindView(R.id.ll_xiala)
    View ll_xiala;
    @BindView(R.id.iv_xiala)
    ImageView iv_xiala;
    @BindView(R.id.ll_jian)
    View ll_jian;
    @BindView(R.id.tv_beishu)
    EditText tv_beishu;
    @BindView(R.id.ll_jia)
    View ll_jia;
    @BindView(R.id.ll_choice)
    View ll_choice;
    @BindView(R.id.ll_biaozhun_guoguan)
    LinearLayout ll_biaozhun_guoguan;
    @BindView(R.id.ll_zuhe_guoguan)
    LinearLayout ll_zuhe_guoguan;
    @BindView(R.id.tvOk)
    TextView tvOk;
    @BindView(R.id.tvSelectWay)
    TextView tvSelectWay;

    FootballMatchSelectedAdapter mAdapter;
    int mCurrentBeishu;
    int mCurrentZhushu;

    private boolean mIsDanguan;

    private List<List<BallMatchBo.MatchInfo>> mSelectedMatchs;
    private int mCurrentBetType;

    private String min_reward, max_reward;

    private static final String[][] ZUHE_GUOGUAN = {{"3", "3", "0"}, {"3", "4", "0_1"},  //2
            {"4", "4", "1"}, {"4", "5", "1_2"}, {"4", "6", "0"}, {"4", "11", "0_1_2"},  //4
            {"5", "5", "2"}, {"5", "6", "2_3"}, {"5", "10", "0"}, {"5", "16", "1_2_3"}, {"5", "20", "0_1"}, {"5", "26", "0_1_2_3"},  //6
            {"6", "6", "3"}, {"6", "7", "3_4"}, {"6", "15", "0"}, {"6", "20", "1"}, {"6", "22", "2_3_4"}, {"6", "35", "0_1"}, {"6", "42", "1_2_3_4"}, {"6", "50", "0_1_2"}, {"6", "57", "0_1_2_3_4"},//9
            {"7", "7", "4"}, {"7", "8", "4_5"}, {"7", "21", "3"}, {"7", "35", "2"}, {"7", "120", "0_1_2_3_4_5"}, //5
            {"8", "8", "5"}, {"8", "9", "5_6"}, {"8", "28", "4"}, {"8", "56", "3"}, {"8", "70", "2"}, {"8", "247", "0_1_2_3_4_5_6"}, //6
    };

    private boolean[] mBZGGState, mTempBZGGState;
    private boolean[] mZHGGState, mTempZHGGState;

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        return false;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_match_calculate_money;
    }

    @Override
    protected void initView() {
        mSelectedMatchs = (List<List<BallMatchBo.MatchInfo>>) DataManager.getInstance().getTempObject();
        DataManager.getInstance().setTempObject(null);
        mCurrentBetType = getIntent().getIntExtra("current_type", 0);

        int selectedMatchNum = getSelectedNum();
        if (selectedMatchNum == 0) {
            finish();
            return;
        }
        mIsDanguan = (selectedMatchNum == 1);
        if (mCurrentBetType < 7 && !mIsDanguan) {
            int count = selectedMatchNum - 1 < 7 ? selectedMatchNum - 1 : 7;
            mBZGGState = new boolean[count];
            mTempBZGGState = new boolean[count];
            if (isHadSelectBifen() && count > 3) {
                count = 3;
            }
            mBZGGState[count - 1] = true;

            count = getZuheCountBySelectedMatchNum(selectedMatchNum);
            mZHGGState = new boolean[count];
            mTempZHGGState = new boolean[count];
        }

        mCurrentBeishu = 1;
        tv_beishu.setText(String.valueOf(mCurrentBeishu));
        resetMoney();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        getAdapter().bindToRecyclerView(recyclerView);
        getAdapter().setNewData(mSelectedMatchs);
    }

    @Override
    protected void initEvent() {
        tv_beishu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    mCurrentBeishu = Integer.valueOf(s.toString());
                } else {
                    mCurrentBeishu = 0;
                }
                resetMoney();
            }
        });
    }

    public void onResume() {
        super.onResume();
        getAdapter().notifyDataSetChanged();
    }


    private FootballMatchSelectedAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new FootballMatchSelectedAdapter(this, mCurrentBetType);
            mAdapter.setOnMatchSelectListener(new FootballMatchSelectedAdapter.OnMatchSelectListener() {
                @Override
                public void showPopupWindow(int currentType, BallMatchBo.MatchInfo matchInfo, TextView tv) {
                    showSelectDialog(currentType, matchInfo, tv);
                }

                @Override
                public void reset() {
                    resetMoney();
                }
            });
        }
        return mAdapter;
    }

    public void showSelectDialog(int currentBetType, BallMatchBo.MatchInfo matchInfo, TextView tv) {
        final MyDialogFragment dialogFragment = new MyDialogFragment();
        if (currentBetType == 2) {
            dialogFragment.setLayout(R.layout.popup_select_football_bifen);
        } else {
            dialogFragment.setLayout(R.layout.popup_select_football_banquanchang);
        }
        dialogFragment.setOnMyDialogListener(new MyDialogFragment.OnMyDialogListener() {
            @Override
            public void initView(View contentView) {
                ((TextView) contentView.findViewById(R.id.tv_1_1)).setText(Html.fromHtml(matchInfo.getHn()));
                ((TextView) contentView.findViewById(R.id.tv_1_3)).setText(Html.fromHtml(matchInfo.getAn()));
                int peilvIndex = 0;
                if (currentBetType == 2) {
                    peilvIndex = initSelectView(currentBetType, (LinearLayout) ((LinearLayout) contentView.findViewById(R.id.ll_sheng)).getChildAt(1), matchInfo, peilvIndex);
                    peilvIndex = initSelectView(currentBetType, (LinearLayout) ((LinearLayout) contentView.findViewById(R.id.ll_ping)).getChildAt(1), matchInfo, peilvIndex);
                    initSelectView(currentBetType, (LinearLayout) ((LinearLayout) contentView.findViewById(R.id.ll_fu)).getChildAt(1), matchInfo, peilvIndex);
                } else {
                    initSelectView(currentBetType, (LinearLayout) ((LinearLayout) contentView.findViewById(R.id.ll)).getChildAt(1), matchInfo, peilvIndex);
                }
                dialogFragment.setDialogViewsOnClickListener(contentView, R.id.tv_cancel, R.id.tv_ok);
            }

            @Override
            public void onViewClick(int viewId) {
                if (viewId == R.id.tv_ok) {
                    ArrayList<String> list = matchInfo.getSpTypeGg();
                    String[] dataType;
                    if (currentBetType == 2) {
                        dataType = DataServer.FOOT_BALL_BF;
                    } else if (currentBetType == 4) {
                        dataType = DataServer.FOOT_BALL_BQC;
                    } else {
                        dataType = DataServer.FOOT_BALL_BQC;
                    }
                    String text = "";
                    int size = list.size();
                    for (int i = 0; i < size; ++i) {
                        matchInfo.setCheck(i, matchInfo.isTempCheck(i));
                        if (matchInfo.isCheck(i)) {
                            text += dataType[i] + "[" + list.get(i) + "] ";
                        }
                    }
                    if (TextUtils.isEmpty(text)) {
                        tv.setText("点击投注");
                        tv.setTextSize(13);
                        tv.setBackgroundResource(R.drawable.bg_touzhu);
                        tv.setTextColor(getResources().getColor(R.color.text_remind_2));
                    } else {
                        tv.setText(text);
                        tv.setTextSize(10);
                        tv.setBackgroundResource(R.drawable.bg_ok);
                        tv.setTextColor(getResources().getColor(R.color.primary_comparison));
                    }
                    resetMoney();
                }
            }
        });
        dialogFragment.show(getSupportFragmentManager(), "MyDialogFragment");
    }

    private int initSelectView(int currentBetType, LinearLayout llParent, BallMatchBo.MatchInfo matchInfo, int currentIndex) {
        int childCount = llParent.getChildCount();
        LinearLayout ll, itemView;
        Resources resources = getResources();
        ArrayList<String> list = matchInfo.getSpTypeGg();
        String[] dataType;
        if (currentBetType == 2) {
            dataType = DataServer.FOOT_BALL_BF;
        } else if (currentBetType == 4) {
            dataType = DataServer.FOOT_BALL_BQC;
        } else {
            dataType = DataServer.FOOT_BALL_BQC;
        }
        for (int i = 0; i < childCount; ++i) {
            ll = (LinearLayout) llParent.getChildAt(i);
            for (int j = 0; j < ll.getChildCount(); ++j) {
                matchInfo.setTempCheck(currentIndex, matchInfo.isCheck(currentIndex));
                itemView = (LinearLayout) ll.getChildAt(j);
                itemView.setTag(currentIndex);
                itemView.setTag(R.id.tag, matchInfo);
                ((TextView) itemView.getChildAt(0)).setText(dataType[currentIndex]);
                ((TextView) itemView.getChildAt(1)).setText(list.get(currentIndex));
                if (matchInfo.isTempCheck(currentIndex)) {
                    itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                    ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                } else {
                    itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                    ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                }
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int currentIndex = (int) view.getTag();
                        BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                        matchInfo.setTempCheck(currentIndex, !matchInfo.isTempCheck(currentIndex));
                        LinearLayout itemView = (LinearLayout) view;
                        if (matchInfo.isTempCheck(currentIndex)) {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                        } else {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                        }
                    }
                });
                ++currentIndex;
            }
        }
        return currentIndex;
    }

    private void resetMoney() {
        mCurrentZhushu = 0;
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = getCheckedMatches();
        int selectedMatchNum = selectedMatchs.size();
        double minPeilv = -1, maxPeilv = 0.0;
        String text = "";
        if (mCurrentBetType < 7 && !mIsDanguan) {
            if (selectedMatchNum > 1) {

                if (isHadSelectBifen() && selectedMatchNum > 4) {
                    boolean isHad = false;
                    int length = mBZGGState.length;
                    for (int i = 3; i < length; ++i) {
                        if (mBZGGState[i]) {
                            isHad = true;
                            break;
                        }
                    }
                    if (!isHad) {
                        length = mZHGGState.length;
                        for (int i = 6; i < length; ++i) {
                            if (mZHGGState[i]) {
                                isHad = true;
                                break;
                            }
                        }
                    }
                    if (isHad) {
                        length = mBZGGState.length;
                        for (int i = 0; i < length; ++i) {
                            mBZGGState[i] = false;
                        }
                        mBZGGState[2] = true;
                        length = mZHGGState.length;
                        for (int i = 0; i < length; ++i) {
                            mZHGGState[i] = false;
                        }
                    }
                }

                List<Integer> intList = new ArrayList<>();
                List<List<BallMatchBo.MatchInfo>> matchs = new ArrayList<>();
                List<List<Integer>> list = new ArrayList<>();

                List<List<BallMatchBo.MatchInfo>> selectedDanMatchs = getSelectedDanMatches();
                int length = mBZGGState.length;
                for (int i = 0; i < length; ++i) {
                    if (mBZGGState[i]) {
                        if (selectedMatchNum >= 2 + i && 2 + i > selectedDanMatchs.size()) {
                            List<List<BallMatchBo.MatchInfo>> unSelectedDanMatchs = getUnSelectedDanMatches();
                            int unSelectDanNum = unSelectedDanMatchs.size();
                            intList.clear();
                            for (int ii = 0; ii < unSelectDanNum; ++ii) {
                                intList.add(ii);
                            }
                            list.clear();
                            combinerSelect(list, intList, new ArrayList<>(), 2 + i - selectedDanMatchs.size());  //用integer节省空间
                            for (List<Integer> integers : list) {
                                matchs.clear();
                                matchs.addAll(selectedDanMatchs);
                                for (Integer integer : integers) {
                                    matchs.add(unSelectedDanMatchs.get(integer));
                                }
                                MatchCalculateInfo info = getBZGGMatchCalculateInfo(0, matchs.size(), 2 + i, matchs);
                                if (info != null) {
                                    mCurrentZhushu += info.count;
                                    if (minPeilv < 0 || minPeilv > info.minPeilv) {
                                        minPeilv = info.minPeilv;
                                    }
                                    maxPeilv += info.maxPeilv;
                                }
                            }
                            text += (2 + i) + "串1, ";
                        } else {
                            mBZGGState[i] = false;
                        }
                    }
                }
                if (text.length() > 2) {
                    text = text.substring(0, text.length() - 2);
                }
                if (selectedDanMatchs.size() == 0) {
                    length = mZHGGState.length;
                    int selectNum, chuanInt;
                    String[] strs;
                    MatchCalculateInfo info = null;

                    intList.clear();
                    for (int ii = 0; ii < selectedMatchNum; ++ii) {
                        intList.add(ii);
                    }
                    for (int i = 0; i < length; ++i) {
                        if (mZHGGState[i]) {
                            text += (ZUHE_GUOGUAN[i][0] + "串" + ZUHE_GUOGUAN[i][1]);
                            selectNum = Integer.parseInt(ZUHE_GUOGUAN[i][0]);
                            list.clear();
                            combinerSelect(list, intList, new ArrayList<Integer>(), selectNum);  //用integer节省空间
                            strs = null;
                            chuanInt = 0;
                            if (ZUHE_GUOGUAN[i][2].contains("_")) {
                                strs = ZUHE_GUOGUAN[i][2].split("_");
                            } else {
                                chuanInt = Integer.parseInt(ZUHE_GUOGUAN[i][2]);
                            }
                            for (List<Integer> integers : list) {
                                matchs.clear();
                                for (Integer integer : integers) {
                                    matchs.add(selectedMatchs.get(integer));
                                }
                                if (strs == null) {
                                    info = getBZGGMatchCalculateInfo(0, matchs.size(), chuanInt + 2, matchs);
                                    if (info != null) {
                                        mCurrentZhushu += info.count;
                                        if (minPeilv < 0 || minPeilv > info.minPeilv) {
                                            minPeilv = info.minPeilv;
                                        }
                                        maxPeilv += info.maxPeilv;
                                    }
                                } else {
                                    for (String str : strs) {
                                        info = getBZGGMatchCalculateInfo(0, matchs.size(), Integer.parseInt(str) + 2, matchs);
                                        if (info != null) {
                                            mCurrentZhushu += info.count;
                                            if (minPeilv < 0 || minPeilv > info.minPeilv) {
                                                minPeilv = info.minPeilv;
                                            }
                                            maxPeilv += info.maxPeilv;
                                        }
                                    }
                                }
                            }
                            minPeilv *= (selectedMatchNum - selectNum + 1);
                        }
                    }
                }
                intList.clear();
                matchs.clear();
                list.clear();
                if (mCurrentZhushu == 0 && selectedDanMatchs.isEmpty()) {
                    length = mBZGGState.length;
                    for (int i = 0; i < length; ++i) {
                        mBZGGState[i] = selectedMatchNum == 2 + i;
                    }
                    length = mZHGGState.length;
                    for (int i = 0; i < length; ++i) {
                        mZHGGState[i] = false;
                    }
                    resetMoney();
                    return;
                }
            } else {
                minPeilv = 0.0;
                maxPeilv = 0.0;
            }
        } else {
            text = "单关";
            double peilv;
            boolean isCanBuy = true;
            List<BallMatchBo.MatchInfo> list;
            for (int i = 0; i < selectedMatchNum; ++i) {
                list = mSelectedMatchs.get(i);
                if (mIsDanguan) {
                    for (BallMatchBo.MatchInfo matchInfo : list) {
                        if (matchInfo.getPlay() == null) {
                            if (matchInfo.getCheckedNum() > 0 && !matchInfo.isDanguan()) {
                                text = "场次不足";
                                isCanBuy = false;
                                break;
                            }
                        } else {
                            if (matchInfo.getPlay().getCheckedNum() > 0 && !matchInfo.isDanguan()) {
                                text = "场次不足";
                                isCanBuy = false;
                                break;
                            }
                        }
                    }
                }
                if (!isCanBuy) {
                    minPeilv = 0.0;
                    maxPeilv = 0.0;
                    mCurrentZhushu = 0;
                    break;
                }
                mCurrentZhushu += getCheckedNum(list);
                peilv = getCheckedPeilv(list, false);
                if (minPeilv < 0.0 || minPeilv > peilv) {
                    minPeilv = peilv;
                }
                peilv = getCheckedPeilv(list, true);
                maxPeilv += peilv;
            }
        }

        if (TextUtils.isEmpty(text)) {
            text = "选择过关方式";
        }
        tvSelectWay.setText(text);
        if (minPeilv < 0 || minPeilv == Double.MAX_VALUE) {
            minPeilv = 0.0;
        }
        tv_money.setText(Html.fromHtml("共<font color=\"#D83220\">" + mCurrentZhushu + "</font>注 "
                + "<font color=\"#D83220\">" + mCurrentBeishu + "</font>倍 "
                + "<font color=\"#D83220\">" + (mCurrentZhushu * mCurrentBeishu * 2) + "</font>元"));

        min_reward = DataServer.getMoneyValue(minPeilv * mCurrentBeishu * 2);
        max_reward = DataServer.getMoneyValue(maxPeilv * mCurrentBeishu * 2);

        tv_award.setText("预计奖金:" + min_reward + "-" + max_reward);
    }


    public void combinerSelect(List<List<Integer>> list, List<Integer> data, List<Integer> workSpace, int k) {
        List<Integer> copyData;
        List<Integer> copyWorkSpace;

        if (workSpace.size() == k) {
            list.add(workSpace);
        }
        for (int i = 0; i < data.size(); i++) {
            copyData = new ArrayList<>(data);
            copyWorkSpace = new ArrayList<>(workSpace);

            copyWorkSpace.add(copyData.get(i));
            for (int j = i; j >= 0; j--) {
                copyData.remove(j);
            }
            combinerSelect(list, copyData, copyWorkSpace, k);
        }
    }


    private MatchCalculateInfo getBZGGMatchCalculateInfo(int index, int totalMatchNum, int num, List<List<BallMatchBo.MatchInfo>> list) {
        if (num == 0) {
            MatchCalculateInfo fo = new MatchCalculateInfo();
            fo.count = 1;
            fo.maxPeilv = fo.minPeilv = 1.0;
            return fo;
        }
        if (index == totalMatchNum) {
            return null;
        }
        MatchCalculateInfo info = null;
        int tempCount = 0;
        double minPeilv = Double.MAX_VALUE, maxPeilv = 0.0, peilv;
        int checkNum;
        for (int i = index; i < totalMatchNum; ++i) {
            checkNum = getCheckedNum(list.get(i));
            info = getBZGGMatchCalculateInfo(i + 1, totalMatchNum, num - 1, list);
            if (info == null) {
                tempCount += 0;
                //minPeilv = 0.0;
            } else {
                tempCount += checkNum * info.count;
                peilv = getCheckedPeilv(list.get(i), false) * info.minPeilv;
                if (peilv < minPeilv) {
                    minPeilv = peilv;
                }
                maxPeilv += getCheckedPeilv(list.get(i), true) * info.maxPeilv;
            }
        }
        if (info == null) {
            info = new MatchCalculateInfo();
        }
        info.count = tempCount;
        info.minPeilv = minPeilv;
        info.maxPeilv = maxPeilv;
        return info;
    }

    private List<List<BallMatchBo.MatchInfo>> getSelectedDanMatches() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs != null && !mSelectedMatchs.isEmpty()) {
            for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                if (list.get(0).isDan()) {
                    selectedMatchs.add(list);
                }
            }
        }
        return selectedMatchs;
    }

    private List<List<BallMatchBo.MatchInfo>> getUnSelectedDanMatches() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs != null && !mSelectedMatchs.isEmpty()) {
            for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                if (!list.get(0).isDan()) {
                    selectedMatchs.add(list);
                }
            }
        }
        return selectedMatchs;
    }

    private List<List<BallMatchBo.MatchInfo>> getCheckedMatches() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs != null && !mSelectedMatchs.isEmpty()) {
            for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                for (BallMatchBo.MatchInfo matchInfo : list) {
                    if (matchInfo.getIsCheck() != null) {
                        if (matchInfo.isHadChecked()) {
                            selectedMatchs.add(list);
                            break;
                        }
                    } else if (matchInfo.getPlay() != null && matchInfo.getPlay().getIsCheck() != null) {
                        if (matchInfo.getPlay().isHadChecked()) {
                            selectedMatchs.add(list);
                            break;
                        }
                    }
                }
            }
        }
        return selectedMatchs;
    }


    private int getCheckedNum(List<BallMatchBo.MatchInfo> list) {
        int checkedNum = 0;
        for (BallMatchBo.MatchInfo matchInfo : list) {
            if (matchInfo.getIsCheck() != null) {
                checkedNum += matchInfo.getCheckedNum();
            } else if (matchInfo.getPlay() != null && matchInfo.getPlay().getIsCheck() != null) {
                checkedNum += matchInfo.getPlay().getCheckedNum();
            }
        }
        return checkedNum;
    }

    private double getCheckedPeilv(List<BallMatchBo.MatchInfo> list, boolean isMaxPeilv) {
        double peilv = -1, peilv2;
        double shengPeilv = -1, pingPeilv = -1, fuPeilv = -1;
        int index = 0;
        for (BallMatchBo.MatchInfo matchInfo : list) {
            if (matchInfo.getIsCheck() != null) {
                peilv = isMaxPeilv ? matchInfo.getCheckedMaxPeilv() : matchInfo.getCheckedMinPeilv();
            } else if (matchInfo.getPlay() != null && matchInfo.getPlay().getIsCheck() != null) {
                if (isMaxPeilv) {
                    if (index == 1 || index == 4) {
                        ++index;
                        continue;
                    }
                    double[] peilvs = getPeilv(matchInfo, index);
                    if (shengPeilv > 0) {
                        shengPeilv += peilvs[0];
                    } else {
                        shengPeilv = peilvs[0];
                    }
                    if (pingPeilv > 0) {
                        pingPeilv += peilvs[1];
                    } else {
                        pingPeilv = peilvs[1];
                    }
                    if (fuPeilv > 0) {
                        fuPeilv += peilvs[2];
                    } else {
                        fuPeilv = peilvs[2];
                    }
                } else {
                    peilv2 = matchInfo.getPlay().getCheckedMinPeilv();
                    if (peilv2 == 0.0) {
                        ++index;
                        continue;
                    }
                    if (peilv < 0 || peilv2 < peilv) {
                        peilv = peilv2;
                    }
                }
            }
            ++index;
        }
        if (isMaxPeilv && list.size() > 1) {
            double[] tempPeilvs = getMaxPeilvBetweenRangqiuBifen(list.get(1), list.get(4));
            double[] bifenPeilvs = getPeilv(list.get(4), 4);
            int hand = Integer.parseInt(list.get(1).getPlay().getHand());
            if (tempPeilvs == null) {
                tempPeilvs = bifenPeilvs;
            } else {
                if (hand <= -1) {
                    if (tempPeilvs[0] < tempPeilvs[1]) {
                        tempPeilvs[0] = tempPeilvs[1];
                        tempPeilvs[1] = tempPeilvs[2];
                    }
                } else if (hand >= 1) {
                    if (tempPeilvs[2] < tempPeilvs[1]) {
                        tempPeilvs[2] = tempPeilvs[1];
                        tempPeilvs[1] = tempPeilvs[0];
                    }
                }
                if (tempPeilvs[0] < bifenPeilvs[0]) {
                    tempPeilvs[0] = bifenPeilvs[0];
                }
                if (tempPeilvs[1] < bifenPeilvs[1]) {
                    tempPeilvs[1] = bifenPeilvs[1];
                }
                if (tempPeilvs[2] < bifenPeilvs[2]) {
                    tempPeilvs[2] = bifenPeilvs[2];
                }
            }
            if (shengPeilv > 0) {
                shengPeilv += tempPeilvs[0];
            } else {
                shengPeilv = tempPeilvs[0];
            }
            if (pingPeilv > 0) {
                pingPeilv += tempPeilvs[1];
            } else {
                pingPeilv = tempPeilvs[1];
            }
            if (fuPeilv > 0) {
                fuPeilv += tempPeilvs[2];
            } else {
                fuPeilv = tempPeilvs[2];
            }
        }
        if (shengPeilv > 0 || pingPeilv > 0 || fuPeilv > 0) {
            if (isMaxPeilv) {
                peilv = shengPeilv;
                if (peilv < pingPeilv) {
                    peilv = pingPeilv;
                }
                if (peilv < fuPeilv) {
                    peilv = fuPeilv;
                }
            }
        }
        if (peilv < 0) {
            peilv = 0;
        }
        return peilv;
    }

    private double[] getPeilv(BallMatchBo.MatchInfo matchInfo, int index) {
        boolean[] isCheckeds = matchInfo.getPlay().getIsCheck();
        double maxPeilv = -1, tempPeilv;
        double[] peilvs = {0, 0, 0};
        int[] array = DataServer.FOOT_BALL_SHENG[index];
        for (int i : array) {
            if (isCheckeds[i]) {
                tempPeilv = Double.parseDouble(matchInfo.getPlay().getSpTypeGg().get(i));
                if (maxPeilv < tempPeilv) {
                    maxPeilv = tempPeilv;
                }
            }
        }
        if (maxPeilv > 0) {
            peilvs[0] += maxPeilv;
        }

        array = DataServer.FOOT_BALL_PING[index];
        maxPeilv = -1;
        for (int i : array) {
            if (isCheckeds[i]) {
                tempPeilv = Double.parseDouble(matchInfo.getPlay().getSpTypeGg().get(i));
                if (maxPeilv < tempPeilv) {
                    maxPeilv = tempPeilv;
                }
            }
        }
        if (maxPeilv > 0) {
            peilvs[1] += maxPeilv;
        }
        array = DataServer.FOOT_BALL_FU[index];
        maxPeilv = -1;
        for (int i : array) {
            if (isCheckeds[i]) {
                tempPeilv = Double.parseDouble(matchInfo.getPlay().getSpTypeGg().get(i));
                if (maxPeilv < tempPeilv) {
                    maxPeilv = tempPeilv;
                }
            }
        }
        if (maxPeilv > 0) {
            peilvs[2] += maxPeilv;
        }
        return peilvs;
    }

    private double[] getMaxPeilvBetweenRangqiuBifen(BallMatchBo.MatchInfo matchInfo1, BallMatchBo.MatchInfo matchInfo2) {
        int hand = Integer.parseInt(matchInfo1.getPlay().getHand());
        double peilv1;

        double[] rangPeilv;
        double[] peilv = {0.0, 0.0, 0.0};
        boolean[] isCheckeds = matchInfo1.getPlay().getIsCheck();
        if (isCheckeds == null || isCheckeds.length == 0) {
            return null;
        }
        int length = isCheckeds.length;
        boolean isHadRangCheck = false;
        for (int i = 0; i < length; ++i) {
            if (isCheckeds[i]) {
                isHadRangCheck = true;
                peilv1 = Double.parseDouble(matchInfo1.getPlay().getSpTypeGg().get(i));
                if (i == 0) {
                    rangPeilv = getRangShengPeilv(hand, matchInfo2.getPlay().getIsCheck(), matchInfo2.getPlay().getSpTypeGg());
                    if (rangPeilv[0] > peilv1 + rangPeilv[1]) {
                        peilv[0] = rangPeilv[0];
                    } else {
                        peilv[0] = peilv1 + rangPeilv[1];
                    }
                } else if (i == 1) {
                    rangPeilv = getRangPingPeilv(hand, matchInfo2.getPlay().getIsCheck(), matchInfo2.getPlay().getSpTypeGg());
                    if (rangPeilv[0] > peilv1 + rangPeilv[1]) {
                        peilv[1] = rangPeilv[0];
                    } else {
                        peilv[1] = peilv1 + rangPeilv[1];
                    }
                } else {
                    rangPeilv = getRangFuPeilv(hand, matchInfo2.getPlay().getIsCheck(), matchInfo2.getPlay().getSpTypeGg());
                    if (rangPeilv[0] > peilv1 + rangPeilv[1]) {
                        peilv[2] = rangPeilv[0];
                    } else {
                        peilv[2] = peilv1 + rangPeilv[1];
                    }
                }
            }
        }
        return isHadRangCheck ? peilv : null;
    }

//    private double[] getBifenPeilv(BallMatchBo.MatchInfo matchInfo) {
//        double[] peilv = {0.0, 0.0, 0.0};
//        double tempPeilv;
//        boolean[] isCheckeds = matchInfo.getPlay().getIsCheck();
//        List<String> peilvs = matchInfo.getPlay().getSpTypeGg();
//        if (isCheckeds != null && isCheckeds.length > 0) {
//            for (int i = 0; i < 13; ++i) {
//                if (isCheckeds[i]) {
//                    tempPeilv = Double.parseDouble(peilvs.get(i));
//                    if (peilv[0] < tempPeilv) {
//                        peilv[0] = tempPeilv;
//                    }
//                }
//            }
//
//            for (int i = 13; i < 18; ++i) {
//                if (isCheckeds[i]) {
//                    tempPeilv = Double.parseDouble(peilvs.get(i));
//                    if (peilv[1] < tempPeilv) {
//                        peilv[1] = tempPeilv;
//                    }
//                }
//            }
//
//            for (int i = 18; i < 31; ++i) {
//                if (isCheckeds[i]) {
//                    tempPeilv = Double.parseDouble(peilvs.get(i));
//                    if (peilv[2] < tempPeilv) {
//                        peilv[2] = tempPeilv;
//                    }
//                }
//            }
//        }
//        return peilv;
//    }

    private double[] getRangShengPeilv(int hand, boolean[] isCheckeds2, List<String> peilvs) {
        double[] rangShengPeilv = {0.0, 0.0};
        double tempPeilv;
        String bifen;
        String[] bifens;
        int detal;
        int length = DataServer.FOOT_BALL_BF.length;
        for (int j = 0; j < length; ++j) {
            if (isCheckeds2[j]) {
                bifen = DataServer.FOOT_BALL_BF[j];
                tempPeilv = Double.parseDouble(peilvs.get(j));
                if (bifen.contains(":")) {
                    bifens = bifen.split(":");
                    detal = Integer.parseInt(bifens[1]) - Integer.parseInt(bifens[0]);
                    if (detal < hand) {
                        if (rangShengPeilv[1] < tempPeilv) {
                            rangShengPeilv[1] = tempPeilv;
                        }
                    }
//                    else {
//                        if (rangShengPeilv[0] < tempPeilv) {
//                            rangShengPeilv[0] = tempPeilv;
//                        }
//                    }
                } else {
                    if (j == 13 || (j == 17 && hand > 0)) {
                        if (rangShengPeilv[1] < tempPeilv) {
                            rangShengPeilv[1] = tempPeilv;
                        }
                    }
                }
            }
        }
        return rangShengPeilv;
    }


    private double[] getRangPingPeilv(int hand, boolean[] isCheckeds2, List<String> peilvs) {
        double[] rangPingPeilv = {0.0, 0.0};
        double tempPeilv;
        String bifen;
        String[] bifens;
        int detal;
        int length = DataServer.FOOT_BALL_BF.length;
        for (int j = 0; j < length; ++j) {
            if (isCheckeds2[j]) {
                bifen = DataServer.FOOT_BALL_BF[j];
                tempPeilv = Double.parseDouble(peilvs.get(j));
                if (bifen.contains(":")) {
                    bifens = bifen.split(":");
                    detal = Integer.parseInt(bifens[1]) - Integer.parseInt(bifens[0]);
                    if (detal == hand) {
                        if (rangPingPeilv[1] < tempPeilv) {
                            rangPingPeilv[1] = tempPeilv;
                        }
                    }
//                    else {
//                        if (rangPingPeilv[0] < tempPeilv) {
//                            rangPingPeilv[0] = tempPeilv;
//                        }
//                    }
                }
            }
        }
        return rangPingPeilv;
    }

    private double[] getRangFuPeilv(int hand, boolean[] isCheckeds2, List<String> peilvs) {
        double[] rangFuPeilv = {0.0, 0.0};
        double tempPeilv;
        String bifen;
        String[] bifens;
        int detal;
        int length = DataServer.FOOT_BALL_BF.length;
        for (int j = 0; j < length; ++j) {
            if (isCheckeds2[j]) {
                bifen = DataServer.FOOT_BALL_BF[j];
                tempPeilv = Double.parseDouble(peilvs.get(j));
                if (bifen.contains(":")) {
                    bifens = bifen.split(":");
                    detal = Integer.parseInt(bifens[1]) - Integer.parseInt(bifens[0]);
                    if (detal > hand) {
                        if (rangFuPeilv[1] < tempPeilv) {
                            rangFuPeilv[1] = tempPeilv;
                        }
                    }
//                    else {
//                        if (rangFuPeilv[0] < tempPeilv) {
//                            rangFuPeilv[0] = tempPeilv;
//                        }
//                    }
                } else {
                    if (j == 30 || (j == 17 && hand < 0)) {
                        if (rangFuPeilv[1] < tempPeilv) {
                            rangFuPeilv[1] = tempPeilv;
                        }
                    }
                }
            }
        }
        return rangFuPeilv;
    }

    @Override
    protected void initInjector() {
        DaggerLottoComponent.builder().lottoPresenterModule(new LottoPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_back, R.id.tvOk, R.id.ll_jian, R.id.ll_jia,
            R.id.tv_buy, R.id.tv_hemai, R.id.ll_xiala, R.id.bgView, R.id.tv_award_youhua, R.id.tv_billing})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.ll_jian:
                if (mCurrentBeishu > 1) {
                    tv_beishu.setText(String.valueOf(--mCurrentBeishu));
                    resetMoney();
                }
                break;
            case R.id.ll_jia:
                tv_beishu.setText(String.valueOf(++mCurrentBeishu));
                resetMoney();
                break;
            case R.id.tv_billing:
            case R.id.tv_hemai:
            case R.id.tv_buy:
                if (ll_choice.getVisibility() == View.VISIBLE) {
                    ll_choice.setVisibility(View.GONE);
                    iv_xiala.setRotation(0);
                    return;
                }
                if (!AM.user().isLogin()) {
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                if (mCurrentZhushu == 0) {
                    T.showShort("请至少选择一注！");
                    return;
                }

                List<List<BallMatchBo.MatchInfo>> selectedMatchs = getCheckedMatches();
                BallMatchBo.MatchInfo matchInfo;
                for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                    if (list.get(0).getPlay() == null) {
                        matchInfo = list.get(0);
                    } else {
                        matchInfo = list.get(0).getPlay();
                    }
                    if (!DataServer.isTimeCanBuy(matchInfo.getSaleCloseTime(), 10 * 60)) {
                        T.showShort(matchInfo.getHn2() + "VS" + matchInfo.getAn2() + "比赛已超出购买时间");
                        return;
                    }
                }

                int betType = mIsDanguan ? 7 : mCurrentBetType;
                int type = 4;
                switch (betType) {
                    case 0://胜负平
                        type = 4;
                        break;
                    case 1://让球胜负平
                        type = 6;
                        break;
                    case 2://比分
                        type = 5;
                        break;
                    case 3://总进球
                        type = 7;
                        break;
                    case 4://半全场
                        type = 8;
                        break;
                    case 5://混头2选1
                        type = 9;
                        break;
                    case 6://混合过关
                        type = 11;
                        break;
                    case 7://单关1
                    case 8://单关2
                        type = 10;
                        break;
                }

                if (id == R.id.tv_hemai) {
                    Intent intent = new Intent(this, FootballMatchHemaiActivity.class);
                    intent.putExtra("total_money", mCurrentZhushu * mCurrentBeishu * 2);
                    intent.putExtra("bet_type", type);
                    intent.putExtra("bet_code", getBetCode());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("issue", getIssue());
                    matchInfo = mSelectedMatchs.get(0).get(0);
                    intent.putExtra("finish_time", matchInfo.getPlay() != null ? matchInfo.getPlay().getSaleCloseTime() : matchInfo.getSaleCloseTime());
                    startActivity(intent);
                } else if (id == R.id.tv_billing) {
                    Intent intent = new Intent(this, BillingActivity.class);
                    intent.putExtra("order_type", type);
                    intent.putExtra("is_order", false);
                    intent.putExtra("data", getBetCode());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("mut_num", mCurrentZhushu);
                    intent.putExtra("issue", getIssue());
                    intent.putExtra("is_follow", 2);
                    intent.putExtra("total_amount", 2 * mCurrentBeishu * mCurrentZhushu);
                    intent.putExtra("is_charsing", 2);
                    intent.putExtra("charsing_num", 1);
                    intent.putExtra("min_reward", min_reward);
                    intent.putExtra("max_reward", max_reward);
                    startActivity(intent);

                } else {
                    Intent intent = new Intent(this, PayActivity.class);
                    intent.putExtra("order_type", type);
                    intent.putExtra("data", getBetCode());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("mut_num", mCurrentZhushu);
                    intent.putExtra("issue", getIssue());
                    intent.putExtra("is_follow", 2);
                    intent.putExtra("total_amount", 2 * mCurrentBeishu * mCurrentZhushu);
                    intent.putExtra("is_charsing", 2);
                    intent.putExtra("charsing_num", 1);
                    intent.putExtra("min_reward", min_reward);
                    intent.putExtra("max_reward", max_reward);
                    startActivity(intent);
                    overridePendingTransition(R.anim.push_bottom_in, R.anim.push_buttom_out);
                }
                break;
            case R.id.bgView:
            case R.id.ll_xiala:
                if (ll_choice.getVisibility() == View.VISIBLE) {
                    ll_choice.setVisibility(View.GONE);
                    iv_xiala.setRotation(0);
                } else {
                    int selectedMatchNum = getSelectedNum();
                    if (mCurrentBetType > 7 || selectedMatchNum <= 1) {
                        return;
                    }
                    ll_choice.setVisibility(View.VISIBLE);
                    iv_xiala.setRotation(180);
                    resetChoiceZuheView();
                }
                break;
            case R.id.tvOk:
                int length = mBZGGState.length;
                for (int i = 0; i < length; ++i) {
                    mBZGGState[i] = mTempBZGGState[i];
                }
                length = mZHGGState.length;
                for (int i = 0; i < length; ++i) {
                    mZHGGState[i] = mTempZHGGState[i];
                }
                ll_choice.setVisibility(View.GONE);
                iv_xiala.setRotation(0);
                resetMoney();
                break;
            case R.id.tv_award_youhua:
                //          T.showShort("暂未开放");
                if (!mIsDanguan && mCurrentBetType < 7) {
                    length = mZHGGState.length;
                    for (int i = 0; i < length; ++i) {
                        if (mZHGGState[i]) {
                            T.showShort("奖金优化不支持组合过关方式\n请选择M串1的过关方式");
                            return;
                        }
                    }
                    boolean hasSelected = false;
                    length = mBZGGState.length;
                    for (int i = 0; i < length; ++i) {
                        if (mBZGGState[i]) {
                            hasSelected = true;
                        }
                    }
                    if (!hasSelected) {
                        T.showShort("请选择M串1的过关方式");
                        return;
                    }
                }
                selectedMatchs = getCheckedMatches();
                if (selectedMatchs.isEmpty()) {
                    T.showShort("请选择比赛");
                    return;
                }
                Intent intent = new Intent(this, FootballBonusOptimizeActivity.class);
                intent.putExtra("matchs", (Serializable) selectedMatchs);
                intent.putExtra("bet_way", mBZGGState);
                intent.putExtra("bet_type", mCurrentBetType);
                intent.putExtra("total_money", mCurrentZhushu * mCurrentBeishu * 2);
                intent.putExtra("beishu", mCurrentBeishu);
                startActivity(intent);
                break;
        }
    }


    private boolean isHadSelectBifen() {
        if (mCurrentBetType == 2) {
            return true;
        } else if (mCurrentBetType == 6) {
            List<List<BallMatchBo.MatchInfo>> selectedMatchs = getCheckedMatches();
            for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                if (list.get(4).getPlay().isHadChecked()) {
                    return true;
                }
            }
        }
        return false;
    }

    private String getBetCode() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = getCheckedMatches();
        StringBuilder bet_code = new StringBuilder();
        boolean isHadDan = false;
        BallMatchBo.MatchInfo matchInfo;
        int size = selectedMatchs.size();
        int length;
        int betType = mIsDanguan ? 7 : mCurrentBetType;
        switch (betType) {
            case 0://胜负平
            case 1://让球胜负平
                for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = selectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    if (matchInfo.isCheck(0)) {
                        bet_code.append("3,");
                    }
                    if (matchInfo.isCheck(1)) {
                        bet_code.append("1,");
                    }
                    if (matchInfo.isCheck(2)) {
                        bet_code.append("0,");
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 2://比分
            case 3://总进球
            case 4://半全场
                for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = selectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    if (matchInfo.getIsCheck() == null) {
                        continue;
                    }
                    length = matchInfo.getIsCheck().length;
                    for (int j = 0; j < length; ++j) {
                        if (matchInfo.isCheck(j)) {
                            bet_code.append(String.valueOf(j + 1));
                            bet_code.append(",");
                        }
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 5://混头2选1
                for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = selectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    int hand = Integer.parseInt(matchInfo.getHand());
                    if (matchInfo.isCheck(0)) {
                        bet_code.append(hand < 0 ? "6-3" : "1-3");
                        bet_code.append(",");
                    }
                    if (matchInfo.isCheck(1)) {
                        bet_code.append(hand < 0 ? "1-0" : "6-0");
                        bet_code.append(",");
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 6://混合过关
            case 7://单关1
            case 8://单关2
                for (List<BallMatchBo.MatchInfo> list : selectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    int size2 = selectedMatchs.get(i).size();
                    boolean isDan = false;
                    int type;
                    for (int j = 0; j < size2; ++j) {
                        matchInfo = selectedMatchs.get(i).get(j);
                        if (matchInfo.getPlay() == null) {
                            type = 0;
                            if (mCurrentBetType == 0) {
                                type = 0;
                            } else if (mCurrentBetType == 1) {
                                type = 1;
                            } else if (mCurrentBetType == 2) {  //比分
                                type = 4;
                            } else if (mCurrentBetType == 3) {//总进球
                                type = 2;
                            } else if (mCurrentBetType == 4) {//半全场
                                type = 3;
                            }
                            isDan = matchInfo.isDan();
                            bet_code.append(matchInfo.getId());
                            bet_code.append(":");
                            if (type == 0 || type == 1) {
                                if (matchInfo.getIsCheck() == null) {
                                    continue;
                                }
                                if (matchInfo.isCheck(0)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-3,");
                                }
                                if (matchInfo.isCheck(1)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-1,");
                                }
                                if (matchInfo.isCheck(2)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-0,");
                                }
                            } else {
                                if (matchInfo.getIsCheck() == null) {
                                    continue;
                                }
                                length = matchInfo.getIsCheck().length;
                                for (int k = 0; k < length; ++k) {
                                    if (matchInfo.isCheck(k)) {
                                        bet_code.append(String.valueOf(type));
                                        bet_code.append("-");
                                        bet_code.append(String.valueOf(k + 1));
                                        bet_code.append(",");
                                    }
                                }
                            }
                        } else {
                            type = Integer.parseInt(matchInfo.getType());
                            if (type == 0 || type == 1) {
                                if (type == 0) {
                                    isDan = matchInfo.isDan();
                                    bet_code.append(matchInfo.getPlay().getId());
                                    bet_code.append(":");
                                }
                                if (matchInfo.getPlay().getIsCheck() == null) {
                                    continue;
                                }
                                if (matchInfo.getPlay().isCheck(0)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-3,");
                                }
                                if (matchInfo.getPlay().isCheck(1)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-1,");
                                }
                                if (matchInfo.getPlay().isCheck(2)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-0,");
                                }
                            } else {
                                if (matchInfo.getPlay().getIsCheck() == null) {
                                    continue;
                                }
                                length = matchInfo.getPlay().getIsCheck().length;
                                for (int k = 0; k < length; ++k) {
                                    if (matchInfo.getPlay().isCheck(k)) {
                                        bet_code.append(String.valueOf(type));
                                        bet_code.append("-");
                                        bet_code.append(String.valueOf(k + 1));
                                        bet_code.append(",");
                                    }
                                }
                            }
                        }
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (isDan) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
        }
        bet_code.append("`");
        if (betType < 7) {
            int index = 0;
            length = mBZGGState.length;
            for (int i = 0; i < length; ++i) {
                if (mBZGGState[i]) {
                    if (index > 0) {
                        bet_code.append(",");
                    }
                    bet_code.append((i + 2) + "c" + 1);
                    ++index;
                }
            }
            length = mZHGGState.length;
            index = 0;
            for (int i = 0; i < length; ++i) {
                if (mZHGGState[i]) {
                    if (index > 0) {
                        bet_code.append(",");
                    }
                    bet_code.append(ZUHE_GUOGUAN[i][0] + "c" + ZUHE_GUOGUAN[i][1]);
                    ++index;
                }
            }
        } else {
            bet_code.append("1c1");
        }
        return bet_code.toString();
    }

    public String getIssue() {
        if (mSelectedMatchs == null || mSelectedMatchs.isEmpty()) {
            return "";
        }
        for (List<BallMatchBo.MatchInfo> item : mSelectedMatchs) {
            if (item.get(0).getPlay() == null) {
                return item.get(0).getIssue();
            } else {
                return item.get(0).getPlay().getIssue();
            }
        }
        return "";
    }

    private int getSelectedNum() {
        if (mSelectedMatchs == null || mSelectedMatchs.isEmpty()) {
            return 0;
        }
        int num = 0;
        for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
            for (BallMatchBo.MatchInfo matchInfo : list) {
                if (matchInfo.getIsCheck() != null) {
                    if (matchInfo.isHadChecked()) {
                        ++num;
                        break;
                    }
                } else if (matchInfo.getPlay() != null && matchInfo.getPlay().getIsCheck() != null) {
                    if (matchInfo.getPlay().isHadChecked()) {
                        ++num;
                        break;
                    }
                }
            }
        }
        return num;
    }

    private int getZuheCountBySelectedMatchNum(int selectedNum) {
        int num = 0;
        switch (selectedNum) {
            case 0:
            case 1:
            case 2:
                num = 0;
                break;
            case 3:
                num = 2;
                break;
            case 4:
                num = 6;
                break;
            case 5:
                num = 12;
                break;
            case 6:
                num = 21;
                break;
            case 7:
                num = 26;
                break;
            default:
                num = 32;
                break;
        }
        return num;
    }

    private void resetChoiceZuheView() {
        int selectedMatchNum = getSelectedNum();
        int danNum = getAdapter().getDanNum();
        resetBiaozhunguoguanView(ll_choice.findViewById(R.id.ll_biaozhun_guoguan), selectedMatchNum, danNum);
        resetZuheguoguanView(ll_choice.findViewById(R.id.ll_zuhe_guoguan), selectedMatchNum, danNum);
    }

    private void resetBiaozhunguoguanView(LinearLayout ll, int selectedMatchNum, int danNum) {
        int biaozhunCount = selectedMatchNum - 1 < 7 ? selectedMatchNum - 1 : 7;
        if (isHadSelectBifen() && biaozhunCount > 3) {
            biaozhunCount = 3;
        }
        int length = mTempBZGGState.length;
        for (int i = 0; i < length; ++i) {
            if (i < biaozhunCount && i + 2 > danNum) {
                mTempBZGGState[i] = mBZGGState[i];
            } else {
                mTempBZGGState[i] = mBZGGState[i] = false;
            }
        }
        int total = danNum == 0 ? biaozhunCount : biaozhunCount - danNum + 1;
        int line = (total % 3 == 0) ? (total / 3) : (total / 3 + 1);
        for (int i = 0; i < ll.getChildCount(); ++i) {
            ll.getChildAt(i).setVisibility(View.GONE);
        }
        LinearLayout itemView;
        TextView tvItem;
        int index;
        for (int i = 0; i < line; ++i) {
            if (i < ll.getChildCount()) {
                itemView = (LinearLayout) ll.getChildAt(i);
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_guoguan_fangshi, null);
                ll.addView(itemView);
            }
            for (int j = 0; j < 3; ++j) {
                tvItem = (TextView) itemView.getChildAt(j);
                index = 3 * i + j + (danNum == 0 ? 0 : danNum - 1);
                if (index < biaozhunCount) {
                    tvItem.setVisibility(View.VISIBLE);
                    tvItem.setText((2 + index) + "串1");
                    tvItem.setTag(index);
                    if (mTempBZGGState[index]) {
                        tvItem.setTextColor(getResources().getColor(R.color.primary_comparison));
                        tvItem.setBackgroundResource(R.drawable.bg_match_filter);
                    } else {
                        tvItem.setTextColor(getResources().getColor(R.color.text_secondary));
                        tvItem.setBackgroundResource(R.drawable.bg_match_not_filter);
                    }

                    tvItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int index = (int) view.getTag();
                            mTempBZGGState[index] = !mTempBZGGState[index];
                            TextView tvItem = (TextView) view;
                            int length = mZHGGState.length;
                            for (int i = 0; i < length; ++i) {
                                mTempZHGGState[i] = false;
                            }
                            resetItemUnChecked(ll_choice.findViewById(R.id.ll_zuhe_guoguan));
                            if (mTempBZGGState[index]) {
                                tvItem.setTextColor(getResources().getColor(R.color.primary_comparison));
                                tvItem.setBackgroundResource(R.drawable.bg_match_filter);
                            } else {
                                tvItem.setTextColor(getResources().getColor(R.color.text_secondary));
                                tvItem.setBackgroundResource(R.drawable.bg_match_not_filter);
                            }
                        }
                    });
                } else {
                    tvItem.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    private void resetZuheguoguanView(LinearLayout ll, int selectedMatchNum, int danNum) {
        int count = getZuheCountBySelectedMatchNum(selectedMatchNum);
        if (isHadSelectBifen() && count > 3) {
            count = 3;
        }
        int length = mTempZHGGState.length;
        for (int i = 0; i < length; ++i) {
            if (danNum > 0) {
                mTempZHGGState[i] = mZHGGState[i] = false;
                continue;
            }
            if (i < count) {
                mTempZHGGState[i] = mZHGGState[i];
            } else {
                mTempZHGGState[i] = mZHGGState[i] = false;
            }
        }
        int line = danNum > 0 ? 0 : (count % 3 == 0) ? (count / 3) : (count / 3 + 1);
        for (int i = 0; i < ll.getChildCount(); ++i) {
            ll.getChildAt(i).setVisibility(View.GONE);
        }
        LinearLayout itemView;
        TextView tvItem;
        for (int i = 0; i < line; ++i) {
            if (i < ll.getChildCount()) {
                itemView = (LinearLayout) ll.getChildAt(i);
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_guoguan_fangshi, null);
                ll.addView(itemView);
            }
            for (int j = 0; j < 3; ++j) {
                tvItem = (TextView) itemView.getChildAt(j);
                if (3 * i + j < count) {
                    tvItem.setVisibility(View.VISIBLE);
                    int index = 3 * i + j;
                    tvItem.setText(ZUHE_GUOGUAN[index][0] + "串" + ZUHE_GUOGUAN[index][1]);
                    tvItem.setTag(index);
                    if (mTempZHGGState[index]) {
                        tvItem.setTextColor(getResources().getColor(R.color.primary_comparison));
                        tvItem.setBackgroundResource(R.drawable.bg_match_filter);
                    } else {
                        tvItem.setTextColor(getResources().getColor(R.color.text_secondary));
                        tvItem.setBackgroundResource(R.drawable.bg_match_not_filter);
                    }
                    tvItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int index = (int) view.getTag();
                            if (mTempZHGGState[index]) {
                                return;
                            }
                            mTempZHGGState[index] = !mTempZHGGState[index];
                            int length = mBZGGState.length;
                            for (int i = 0; i < length; ++i) {
                                mTempBZGGState[i] = false;
                            }
                            length = mZHGGState.length;
                            for (int i = 0; i < length; ++i) {
                                if (i != index) {
                                    mTempZHGGState[i] = false;
                                }
                            }
                            resetItemUnChecked(ll_choice.findViewById(R.id.ll_biaozhun_guoguan));
                            resetItemUnChecked(ll_choice.findViewById(R.id.ll_zuhe_guoguan));
                            TextView tvItem = (TextView) view;
                            if (mTempZHGGState[index]) {
                                tvItem.setTextColor(getResources().getColor(R.color.primary_comparison));
                                tvItem.setBackgroundResource(R.drawable.bg_match_filter);
                            } else {
                                tvItem.setTextColor(getResources().getColor(R.color.text_secondary));
                                tvItem.setBackgroundResource(R.drawable.bg_match_not_filter);
                            }
                        }
                    });
                } else {
                    tvItem.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    private void resetItemUnChecked(LinearLayout ll) {
        int line = ll.getChildCount();
        LinearLayout itemView;
        TextView tvItem;
        for (int i = 0; i < line; ++i) {
            itemView = (LinearLayout) ll.getChildAt(i);
            for (int j = 0; j < 3; ++j) {
                tvItem = (TextView) itemView.getChildAt(j);
                tvItem.setTextColor(getResources().getColor(R.color.text_secondary));
                tvItem.setBackgroundResource(R.drawable.bg_match_not_filter);
            }
        }
    }

    @Override
    public void showCurrentSuccess(LuckyHistoryBo luckyHistoryBo) {

    }

    @Override
    public void showCurrentError(String ex) {

    }

    @Override
    public void showCreateOrderSuccess(String luckyHistoryBo) {
        T.showShort("下单成功！");
        finish();
    }

    @Override
    public void showCreateOrderError(String ex) {

    }

    @Override
    public void showLotteryHistorySuccess(List<LotteryHistoryBo> resultBos) {

    }

    @Override
    public void showLotteryHistoryError(String error) {

    }

    class MatchCalculateInfo {
        int index = 0;
        int count = 0;
        double minPeilv = 0.0;
        double maxPeilv = 0.0;
    }
}
